<?php


Class ApiAction extends Action {

    var $path;
    var $DB_PREFIX;

    public function _initialize() {
        $this->path = "upload/Portrait/";
        $this->DB_PREFIX = C('DB_PREFIX');
    }

    //获取身份
    public function get_identity($uid, $item_id) {
        $map["uid"] = $uid;
        $res = M("members")->where($map)->find();
        if ($res[purview] == 2) {
            return 3;
        }
        if ($res[purview] == 1) {
            return 2;
        }
        $map["item_id"] = $item_id;
        $map["is_manager"] = 1;
        $map['_logic'] = 'and';
        $res = M("collect")->where($map)->find() ? 1 : 0;
        return $res;
    }

    public function get_gag($uid, $item_id) {
        $map["uid"] = $uid;
        $map["item_id"] = $item_id;
        $map['_logic'] = 'and';
        $res = M("gag")->where($map)->find() ? 1 : 0;
        return $res;
    }
    
       public function get_gag_with_username($username, $item_id) {
        $arr=$this->get_members_($username);
        $uid=$arr[uid];
        $map["uid"] = $uid;
        $map["item_id"] = $item_id;
        $map['_logic'] = 'and';
        $res = M("gag")->where($map)->find() ? 1 : 0;
        return $res;
    }

    public function get_title($uid) {
        $map["uid"] = $uid;
        $res = M("members")->field('title')->where($map)->find();
        return $res['title'];
    }

    public function get_members($uid) {
        $map["uid"] = $uid;
        $res = M("members")->where($map)->find();
        return $res;
    }
      public function get_members_($username) {
        $map["username"] = $username;
        $res = M("members")->where($map)->find();
        return $res;
    }


    public function get_version($id) {
        $map["id"] = $id;
        $res = M("version")->where($map)->find();
        return $res;
    }
    public function get_gift_log($to_id) {
        $map["to_id"] = $to_id;
        $res = M("gift_log")->where($map)->find();
        return $res;
    }

    public function get_gift($gift_id) {
        $map["gift_id"] = $gift_id;
        $res = M("gift")->where($map)->find();
        return $res;
    }
    
     public function get_collect($uid) {
        $map["uid"] = $uid;
        $res = M("collect")->where($map)->find();
        return $res;
    }
    
    public function get_collect_sum($item_id) {
        
        $Model = new Model();
        $list = $Model->query("SELECT z_collect.item_id,COUNT(uid)as sum FROM z_collect WHERE z_collect.item_id={$item_id}");
        return $list[0][sum];
    }
   
       public function is_identity_do($uid,$item_id) {
              
            $map["uid"] = $uid;
            $map["item_id"] = $item_id;
             $map["is_manager"] = 1;
            $res = M("collect")->where($map)->find();
            
            $ret = $res ?1 : 0;
            
            return $ret;
         
          
          }
          
            public function is_follow($uid,$item_id) {
            $map["uid"] = $uid;
            $map["item_id"] = $item_id;
            $res = M("collect")->where($map)->find();
            
              $ret = $res ?1 : 0;
            
            return $ret;
        }
        
        
          
           public function is_identity_with_username($username,$item_id) {
              
            $arr=$this->get_members_($username);
            $uid=$arr[uid];
            $map["uid"] = $uid;
            $map["item_id"] = $item_id;
             $map["is_manager"] = 1;
             $map['_logic'] = 'and';
            $res = M("collect")->where($map)->find();
            
             $ret = $res ?1 : 0;
            
            return $ret;
         
          
          }
          
    public function get_grade($grade_level) {
        $map["grade_level"] = $grade_level;
        $res = M("grade")->where($map)->find();
        return $res;
    }

    public function get_level($score) {
        $grade = M("grade")->findAll();

        if ($score < $grade[0]['need_score']) {

            $res = $grade[0]['grade_level'];
        } else {
            for ($i = 1; $i < count($grade); $i++) {

                if ($score >=$grade[$i-1]['need_score']&&$score < $grade[$i]['need_score']) {

                    $res = $grade[$i]['grade_level'];
                }
            }
        }

        return $res;
    }

    public function get_portrait($uid) {
        $map["uid"] = $uid;
        $res = M("members")->field('portrait')->where($map)->find();
        return $res['portrait'];
    }
    
    public function get_category($cate_id) {
        $map["cate_id"] = $cate_id;
         $res = M("category")->where($map)->find();
        return $res;
        
    }

    public function successres($message = "操作成功",$ext='0') {

        $index["status"] = 1;

        $index["message"] = $message;
        
        $index["ext"] = $ext;

        $res = json_encode($index);

        echo $res;
        exit();
    }

    public function faileres($message = "获取失败",$ext='0') {

        $index["status"] = 0;

        $index["message"] = $message;
        
        $index["ext"] = $ext;

        $res = json_encode($index);

        echo $res;
        exit();
    }

}

?>
